package com.weibo.host.dao.imp;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import org.apache.log4j.Logger;

import com.weibo.blacklist.service.BlackListService;
import com.weibo.blacklist.service.imp.BlackListServiceImp;
import com.weibo.common.CommonUtil;
import com.weibo.common.DB;
import com.weibo.favorite.Service.FavoriteService;
import com.weibo.favorite.Service.imp.FavoriteServiceImp;
import com.weibo.favorite.dao.FavoriteDao;
import com.weibo.favorite.dao.imp.FavoriteDaoImp;
import com.weibo.history.service.HistoryService;
import com.weibo.history.service.imp.HistoryServiceImp;
import com.weibo.host.dao.HostDao;
import com.weibo.sql.SqlFile;

public class HostDaoImp implements HostDao {
	
	private static Logger logger =  Logger.getLogger(HostDaoImp.class);
	@Override
	public List getHostList_ByVisitor(int currentPage) {
		// TODO Auto-generated method stub
		if(currentPage<0){
			currentPage = 0;
		}
		String sql = CommonUtil.getPageSql(SqlFile.HOST_VISITOR_DEAFAULT, currentPage);
		return DB.sqlExeQuery(sql);
	}
	@Override
	public int getHostTotalSize(String sql) {
		// TODO Auto-generated method stub
		ResultSet rs = DB.plsqlExec_res(sql);
		int count = 0;
		try {
			if(rs.next()){
				count = rs.getInt(1);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			logger.error(e);
		}
		return count;
	}
	@Override
	public List getHostList_ForVisitor(String sql, int currentPage) {
		// TODO Auto-generated method stub
		if(currentPage<0){
			currentPage = 0;
		}
		String sql_temp = CommonUtil.getPageSql(sql, currentPage);
		return DB.sqlExeQuery(sql);
	}
	@Override
	public List getBlackList(int currentPage,String user_id) {
		// TODO Auto-generated method stub
		BlackListService blackListService = new BlackListServiceImp();
		return blackListService.selectBlackList(currentPage,user_id);
	}
	@Override
	public int getBlackSize(String user_id) {
		// TODO Auto-generated method stub
		int count = 0;
		count = this.getHostTotalSize(CommonUtil.getTotalSizeSql_bySql(CommonUtil.getSqlBy_par(SqlFile.BLACKLIST_SEARCH, user_id)));
		
		return count;
	}
	@Override
	public List getFavoriteList(int currentPage,String user_id) {
		// TODO Auto-generated method stub
		FavoriteService favoriteDao =  new FavoriteServiceImp();
		return favoriteDao.getFavoriteList(currentPage,user_id);
	}
	@Override
	public int getFavoriteSize(String user_id) {
		// TODO Auto-generated method stub
		int count = 0;
		count = this.getHostTotalSize(CommonUtil.getTotalSizeSql_bySql(CommonUtil.getTotalSizeSql_bySql(CommonUtil.getSqlBy_par(SqlFile.FAVORITE_SELECT, user_id))));
		return count;
	}
	@Override
	public List getHistoryList(int currentPage,String user_id) {
		// TODO Auto-generated method stub
		HistoryService historyService = new HistoryServiceImp();
		return historyService.getHistoryList(currentPage,user_id);
	}
	@Override
	public int getHistorySize(String user_id) {
		// TODO Auto-generated method stub
		int count = 0;
		count = this.getHostTotalSize(CommonUtil.getTotalSizeSql_bySql(CommonUtil.getSqlBy_par(SqlFile.HISTORY_SELECT, user_id)));
		return count;
	}
	@Override
	public List getHongRenList(int currentPage) {
		// TODO Auto-generated method stub
		return DB.sqlExeQuery(CommonUtil.getPageSql(SqlFile.HOST_HONGREN_LIST, currentPage));
	}
	@Override
	public int getHongRenSize() {
		// TODO Auto-generated method stub
		int count = 0;
		count = this.getHostTotalSize(CommonUtil.getTotalSizeSql_bySql(SqlFile.HOST_HONGREN_LIST));
		return count;
	}
	@Override
	public List getMingRenList(int currentPage) {
		// TODO Auto-generated method stub
		return DB.sqlExeQuery(CommonUtil.getPageSql(SqlFile.HOST_MINGREN_LIST, currentPage));
	}
	@Override
	public int getMingRenSize() {
		// TODO Auto-generated method stub
		int count = 0;
		count = this.getHostTotalSize(CommonUtil.getTotalSizeSql_bySql(SqlFile.HOST_MINGREN_LIST));
		return count;
	}
	@Override
	public List getReMenList(int currentPage) {
		// TODO Auto-generated method stub
		return DB.sqlExeQuery(CommonUtil.getPageSql(SqlFile.HOST_REMEN_LIST, currentPage));
	}
	@Override
	public int getReMenSize() {
		// TODO Auto-generated method stub
		int count = 0;
		count = this.getHostTotalSize(CommonUtil.getTotalSizeSql_bySql(SqlFile.HOST_REMEN_LIST));
		return count;
	}
	@Override
	public List getZhongdianTuijian(int currentPage) {
		// TODO Auto-generated method stub
		return DB.sqlExeQuery(CommonUtil.getPageSql(SqlFile.HOST_ZHONGDIANTUIJIAN_LIST, currentPage));
	}
	@Override
	public int getZhongdiantuijianSize() {
		// TODO Auto-generated method stub
		int count = 0;
		count = this.getHostTotalSize(CommonUtil.getTotalSizeSql_bySql(SqlFile.HOST_ZHONGDIANTUIJIAN_LIST));
		return count;
	}
	@Override
	public List getHostListBySql(String sql) {
		// TODO Auto-generated method stub
		
		return DB.sqlExeQuery(sql);
	}
	@Override
	public int getHostTotalSizeBysql(String sql) {
		// TODO Auto-generated method stub
		return this.getHostTotalSize(sql);
	}
}
